package com.example.file_management.controller;

import com.example.file_management.pojo.material.Project;
import com.example.file_management.service.ProjectService;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/projects")
@CrossOrigin(origins = "http://localhost:5173")
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    // 创建新项目
    @PostMapping
    public ResponseEntity<Project> createProject(@RequestBody Project project, @RequestParam String username) {
        try {
            int result = projectService.createProject(project, username);
            if (result > 0) {
                return ResponseEntity.ok(project); // 假设返回插入后的project对象，实际应用中可能需要重新查询数据库
            } else {
                return ResponseEntity.status(500).body(null);
            }
        } catch (SecurityException e) {
            return ResponseEntity.status(403).body(null); // 返回403 Forbidden状态码
        }
    }

    // 根据ID获取项目
    @GetMapping("/{id}")
    public ResponseEntity<Project> getProjectById(@PathVariable Long id) {
        Project project = projectService.getProjectById(id);
        if (project != null) {
            return ResponseEntity.ok(project);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 获取所有项目
    @GetMapping("/gets")
    public ResponseEntity<List<Project>> getAllProjects() {
        List<Project> projects = projectService.getAllProjects();
        return ResponseEntity.ok(projects);
    }


    // 更新项目信息
    @PutMapping("/update")
    public ResponseEntity<String> updateProject(@RequestBody Project project, @RequestParam String username) {
        try {
            int result = projectService.updateProject(project, username);
            if (result > 0) {
                return ResponseEntity.ok("项目更新成功");
            } else {
                return ResponseEntity.status(500).body("项目更新失败");
            }
        } catch (SecurityException e) {
            return ResponseEntity.status(403).body("没有足够的权限更新项目");
        }
    }

    // 删除项目
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteProject(@PathVariable Long id, @RequestParam String username) {
        try {
            int result = projectService.deleteProject(id, username);
            if (result > 0) {
                return ResponseEntity.ok("项目删除成功");
            } else {
                return ResponseEntity.status(500).body("项目删除失败");
            }
        } catch (SecurityException e) {
            return ResponseEntity.status(403).body("没有足够的权限删除项目");
        }
    }
}